package ru.ipccenter.jshell.frontend.compiler;

import javax.tools.FileObject;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import java.io.FileNotFoundException;
import java.io.PrintStream;

@SuppressWarnings("unchecked")
public class TmpClasspathFileManager extends ShellFileManager {

  private PrintStream errorStream;
  private String classpath;
  
  
  TmpClasspathFileManager(JavaCompiler compiler, String classpath, PrintStream errorStream) {
    
    super(compiler.getStandardFileManager(null, null, null));
    this.classpath = classpath;
    this.errorStream = errorStream;
  }
  
  @Override
  public SimpleJavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) {
    
      FileByteCode byteCode = null;
      try {
        byteCode = new FileByteCode(name, classpath, kind);
      } catch (FileNotFoundException e) {
        //TODO: change it for logging
        errorStream.println(e.getMessage());
        return new MemoryByteCode(name, kind);
      }
      return byteCode;
  }
}
